package com.dragon.common.spring.openapi.customizers.operation;

import com.dragon.common.spring.openapi.ApiSignatureHelper;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

/**
 * @author Chris Chan
 * Create On 2025/8/6 上午10:55
 * Use for: 接口加签
 * Explain:
 *
 */
@Component
public class ApiSignatureOperationCustomizer implements OperationCustomizer {

    @Override
    public Operation customize(Operation operation, HandlerMethod method) {
        if (method != null) {
            //检查是否支持
            boolean required = ApiSignatureHelper.support(method);

            if (required) {
                // 添加签名域
                operation.addParametersItem(new Parameter().name("X-App-Id").description("应用ID").required(true).in(ParameterIn.HEADER.toString()));
                operation.addParametersItem(new Parameter().name("X-Timestamp").description("时间戳(毫秒)").required(true).in(ParameterIn.HEADER.toString()));
                operation.addParametersItem(new Parameter().name("X-Signature").description("请求签名").required(true).in(ParameterIn.HEADER.toString()));
            }
        }
        return operation;
    }
}
/*
    @Parameters({
        @Parameter(name = "Authorization", description = "访问令牌", required = true, in = ParameterIn.HEADER),
        @Parameter(name = "X-Timestamp", description = "时间戳(毫秒)", required = true, in = ParameterIn.HEADER),
        @Parameter(name = "X-Signature", description = "请求签名", required = true, in = ParameterIn.HEADER)
    })
*/